package com.example.springbott.wx_service;

import com.example.springbott.bean.MarketAddress;
import com.example.springbott.bean.MarketAddressExample;
import com.example.springbott.bean.MarketUser;
import com.example.springbott.common.CommonData;
import com.example.springbott.mapper.MarketAddressMapper;
import com.example.springbott.util.Count;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: zeng
 * @date: 13:46 2022/11/22
 */

@Service
public class WXAddressServiceImpl implements WXAddressService {
    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public CommonData<MarketAddress> list(MarketUser marketUser) {
        Integer id = marketUser.getId();

        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        criteria.andUserIdEqualTo(id).andDeletedEqualTo(false);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(marketAddresses);
        int page = pageInfo.getPageNum();
        int pages = pageInfo.getPages();
        int limit = pageInfo.getPageSize();
        long total = pageInfo.getTotal();

        CommonData<MarketAddress> data = new CommonData<>();
        data.setLimit(limit);
        data.setPage(page);
        data.setPages(pages);
        data.setTotal((int) total);
        data.setList(pageInfo.getList());
        return data;
    }

    @Override
    public MarketAddress detail(Integer id) {

        return marketAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer save(MarketAddress address, MarketUser marketUser) {
        Boolean isDefault = address.getIsDefault();
        Integer id = address.getId();
        //新增或修改时标注为默认地址
        if (isDefault == true) {
            marketAddressMapper.updateIsDefaultByTrue();
        }
        if (id == Count.ZERO) {
            address.setAddTime(new Date());
            address.setUpdateTime(new Date());
            address.setUserId(marketUser.getId());
            address.setPostalCode(address.getAreaCode());
            address.setDeleted(false);
            marketAddressMapper.insertSelective(address);
            Integer addressId = marketAddressMapper.selectReturnId(marketUser.getId(), address.getName(), address.getTel());
            return addressId;
        } else {
            marketAddressMapper.updateByPrimaryKeySelective(address);
            return address.getId();
        }
    }

    @Override
    public void delete(Integer id) {
        //     逻辑删除
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        criteria.andIdEqualTo(id);
        MarketAddress marketAddress = new MarketAddress();
        marketAddress.setDeleted(true);
        marketAddress.setUpdateTime(new Date());
        marketAddressMapper.updateByExampleSelective(marketAddress, marketAddressExample);

    }
}
